<div id="divFirstHeader"></div>  <div class="header">
    <div class="title">
        <?php echo $this->element('print/title'); ?>
    </div>
    <div class="documentTitle">
        <div class="documentNote">VMI: CUSTOMER INVENTORY</div>
        <table>
            <tr>
                <td><label>Printed Date</label></td>
                <td><?php echo date('d/m/Y H:i'); ?></td>
                <td><label>Page</label></td>
                <td><span class="pageNo">1/1</span></td>
            </tr>
            <tr>
                <td><label>Printed by</label></td>
                <td colspan="3"><?php echo $report_by; ?></td>
            </tr>
            <tr>
                <td width="90px"><label>Customer no.</label></td>
                <td colspan="3"><?php echo $customer_no; ?></td>
            </tr>
            <tr>
                <td><label>Customer name</label></td>
                <td colspan="3"><?php echo $customer_name; ?></td>
            </tr>
            <?php if ($session->check('Criteria.dispatch_from')) {
            ?>
                <tr>
                    <td><label>Vendor</label></td>
                    <td colspan="3"><?php echo $session->read('Criteria.dispatch_from'); ?></td>
                </tr>
            <?php } ?>
            <?php if ($session->check('Criteria.brand_name')) {
            ?>
                <tr>
                    <td><label>Brand</label></td>
                    <td colspan="3"><?php echo $session->read('Criteria.brand_name'); ?></td>
                </tr>
            <?php } ?>
            <?php if ($session->check('Criteria.category_name')) {
            ?>
                <tr>
                    <td><label>Category</label></td>
                    <td colspan="3"><?php echo $session->read('Criteria.category_name'); ?></td>
                </tr>
            <?php } ?>
            <?php if ($session->check('Criteria.stock_code')) {
            ?>
                <tr>
                    <td><label>Stock Code</label></td>
                    <td colspan="3"><?php echo $session->read('Criteria.stock_code'); ?></td>
                </tr>
            <?php } ?>
            <?php if ($session->check('Criteria.part_no')) {
            ?>
                <tr>
                    <td><label>Vendor Part no.</label></td>
                    <td colspan="3"><?php echo $session->read('Criteria.part_no'); ?></td>
                </tr>
            <?php } ?>
        </table>
    </div>
</div>

<div class="main">
    <table id="dataTable" class="dataTable" cellspacing="0" cellpadding="0" >
        <thead id="tableHead">
            <tr align="center">
                <th class="no">No</th>
                <th width="100px">Stock Code</th>
                <th width="100px">Vendor Part no.</th>
                <th>Description</th>
                <?php foreach ($sites as $site) : ?>
                    <th width="60px"><?php echo $site['Site']['name']; ?></th>
                <?php endforeach; ?>
                    <th width="50px">Total</th>
                </tr>
            </thead>
            <tbody id="tableBody">
            <?php
                    $site_id = $session->read('Auth.User.site_id');
                    foreach ($this->data as $i => $data):
            ?>
                        <tr id="row<?php echo $i; ?>" class="row">
                            <td class="center rowTd"><?php echo ($i + 1); ?></td>
                            <td><?php
                        echo $data[$site_id]['CustProductSite']['stock_code'];
            ?></td>
                    <td>
                    <?php
                        // หาว่า Product นี้ Dispatch from มาจากไหน
                        $dispatch_from;
                        // Site แรกที่มี CustProductSite ใน Product นี้
                        $first_site_id;
                        $total_stock = 0;
                        for ($s = 0; $s < count($sites); ++$s) {
                            if (!empty($data[$sites[$s]['Site']['id']])) {
                                $dispatch_from = $data[$sites[$s]['Site']['id']]['CustProductSite']['dispatch_from'];
                                $first_site_id = $sites[$s]['Site']['id'];
                                break;
                            }
                        }
                        echo $dispatch_from == ITS_PREFIX ? $data['Product']['part_no'] : $data['Product']['supplier_part_no'];
                    ?>
                    </td>
                    <td><?php echo!empty($data[$first_site_id]['CustProductSite']['text']) ? $data[$first_site_id]['CustProductSite']['text'] : $data['Product']['title']; ?></td>
                <?php foreach ($sites as $site) : ?>
                            <td class="text-right">
                    <?php
                            if (!empty($data[$site['Site']['id']])) {
                                echo $data[$site['Site']['id']]['CustProductSite']['current_stock'];
                                $total_stock += $data[$site['Site']['id']]['CustProductSite']['current_stock'];
                            } else {
                                echo '-';
                            }
                    ?>
                        </td>
                <?php endforeach; ?>
                            <td class="text-right bold"><?php echo $total_stock; ?></td>
                        </tr>
            <?php endforeach; ?>

            <?php for ($i = 0; $i < 1; ++$i) : ?>
                                <tr class="emptyRow"> <!-- สร้างแถวเปล่าขึ้นมาเพื่อยืดขนาดตารางให้เต็มหน้า-->
                                    <td class="rowTd">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                <?php foreach ($sites as $site) {
                ?>
                                    <td>&nbsp;</td>                    
                <?php } ?>
                            </tr>
            <?php endfor; ?>
        </tbody>
    </table>
</div>

<div class="footer"></div>

<div id="divLastFooter">
</div>